package com.solve.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.solve.dao.ResourceInfoCommentMapper;
import com.solve.dao.ResourceInfoUserMapper;
import com.solve.entity.ResourceInfoComment;

import com.solve.entity.ResourceInfoCommentExample;
import com.solve.entity.ResourceInfoUser;
import com.solve.entity.User;
import com.solve.remote.UserRemoteService;
import com.solve.shiro.JWTToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class ResourceInfoCommentServiceImpl {

    @Resource
    private ResourceInfoCommentMapper resourceInfoCommentMapper;

    @Resource
    private UserRemoteService userRemoteService;

    @Resource
    private ResourceInfoUserMapper resourceInfoUserMapper;

  public ResourceInfoComment saveResourceInfoComment(ResourceInfoComment resourceInfoComment){

            JWTToken jwtToken = (JWTToken) SecurityUtils.getSubject().getPrincipal();
            resourceInfoComment.setCreatorid(jwtToken.getUserId());
            resourceInfoComment.setType("1");
            resourceInfoComment.setUuid(UUID.randomUUID().toString());
            User user = userRemoteService.getUserAllInfo(jwtToken.getUserId());
            resourceInfoComment.setUser(user);
            resourceInfoCommentMapper.insert(resourceInfoComment);
            //计算当前资源评分信息,通国际
            ResourceInfoUser resourceInfoUser = new ResourceInfoUser();
            resourceInfoUser.setUuid(resourceInfoComment.getResourceId());
            resourceInfoUserMapper.updateResourceInfoGrade(resourceInfoUser);//更新资源评分

        return resourceInfoComment;
    }

    public PageInfo selectResourceInfoCommentList(ResourceInfoComment resourceInfoComment){
        List<ResourceInfoComment> resourceInfoCommentList =  resourceInfoCommentMapper.selectResourceCommentWithUser(resourceInfoComment);
        PageInfo<ResourceInfoComment> pageInfo = new PageInfo<ResourceInfoComment>(resourceInfoCommentList);
        return pageInfo;
    }
}
